var loginid,loginuser,loginname,loginkey;
var is_use=0;//是否使用
var is_export=0;//是否冻结
var card_cami="";
var isjump = 0;//控制翻页

var export_id="";
layui.config({
	version:'1510164124181'
}).use(['form','laydate', 'laypage', 'layer', 'element'],function(){
	form = layui.form;
	layer = layui.layer;
	$ = layui.jquery;
    laydate = layui.laydate;//日期
    laypage = layui.laypage;//分页
    element = layui.element; //元素操作
	loginid = $.session.get("userid");
	loginuser = $.session.get("admin_realname");
	loginname = $.session.get("username");
	loginkey = $.session.get("user_secretkey");
	//加载默认数据
 	/*if(window.sessionStorage.getItem("refreshdata")){
 		var data = JSON.parse(window.sessionStorage.getItem("refreshdata"));
		pageNo = data.pageNo;
 	}*/
	myrefresh();
});

////查询所有 是否冻结  是否使用的体验卡
var initdata=[];
var refreshdata;

function inituserdata(pageRecords,pageNo){
	//refreshdata+='{"pageNo":"'+pageNo+'"}';
	is_use=$("#user_type option:selected").val();
	is_export=$("#forcard_type option:selected").val();
	card_cami=$("#card_camis").val();
	//window.sessionStorage.setItem("refreshdata",refreshdata);
	//alert(loginname + "','" + loginkey + "','" + is_use + "','" + is_export + "','" + card_cami + "','"+pageRecords+"','"+pageNo);
	$.ajax({
		url: "../../../../service",
		cache: false,
		data: {
			proc: "xmy_zyr.find_use_forcard",
			para: "['" + loginname + "','" + loginkey + "','" + is_use + "','" + is_export + "','" + card_cami + "','"+pageRecords+"','"+pageNo+"']"
		},
		dataType: "json",
		success: function(data) {
			//console.log(JSON.stringify(data));
			if(data[0].TAG=='0'){
				initdata=[];
				top.layer.alert(""+data[0].MSG+"",function(index){
				   top.location.href="login.html";
				   layer.close(index);
			    });
			}else if(data[0].TAG=='2'){
			    initdata=[];
			}else if(data[0].msg=='读取接口失败,参数错误'||data.msg=='读取接口失败,参数错误'){
				initdata=[];
			}else{
			    initdata =data;
			}
			var dataHtml="";
			$(".data_content").html("");
			if(initdata.length==0){
				  totalRecords =0;
				  dataHtml += '<tr>'
					+'<td colspan="12" style="text-align:center;">暂无数据</td>'
					+'</tr>';
				page();
			}else{
				for(var i=0;i<initdata.length;i++){
					if(i==initdata.length-1){
						totalRecords = initdata[i].ID;
						totalPage = initdata[i].IS_USE;
					}else{
						export_id+=initdata[i].ID+",";
						dataHtml+="<tr>"+
								"<td>"+initdata[i].ID+"</td>"+
								"<td>"+initdata[i].CARDS_NUMBER+"</td>"+
								"<td>"+initdata[i].CARDS_PASSWORD+"</td>"+
								"<td>"+initdata[i].CARDS_VALUE+"元</td>"+
								"<td>"+initdata[i].CARDS_CREATE_TIME+"</td>"+
								"<td>"+initdata[i].WHETHER_TO_EXPORT+"</td>"+
								"<td>"+initdata[i].IS_USE+"</td>"+
								"<td>"+initdata[i].TIMES+"</td>"+
								"<td><a href='#' id='"+initdata[i].ID+"' onclick='deletecard(this);'>删除</a></td>"+
							"</tr>";

					}
				}
			}
			$("tbody").html(dataHtml);
			  layer.closeAll();
            //执行一个laypage实例,保证每个方法只进一次
            if(isjump==0){
				page();
			}
           isjump++;

		}

	});

}
//分页
function page(){
	laypage.render({
		elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
		,count:totalRecords //数据总数，从服务端得到
		,curr: pageNo //获取起始页 location.hash.replace('#!fenye=', '')
        ,hash: 'fenye' //自定义hash值
		,layout: ['count', 'prev', 'page', 'next', 'skip']
		,jump: function(obj, first){
			//首次不执行
			if(!first){
			  //console.log("当前页==="+obj.curr); //得到当前页，以便向服务端请求对应页的数据。
			  //console.log("每页显示条数==="+obj.limit); //得到每页显示的条数
			  pageNo = obj.curr;
			  inituserdata(obj.limit,obj.curr);
			}
		  }
	});
}
function delete_exeropt(ids){
	top.layer.confirm('清除所有体验卡数据？清除后不可恢复',{icon: 3,title:'提示信息'},function(index){
	var index = top.layer.msg('删除中，请稍候',{icon: 16,time:false,shade:0.8});
		$.ajax({
			url: "../../../../service",
			cache: false,
			async: false,
			data: {
				proc: "xmy_zyr.delete_exeropt",
				para: "['" + loginname + "','" + loginkey + "','"+ids+"']"
			},
			dataType: "json",
			success: function(data) {
				if(data[0].TAG=='1'){
					layer.closeAll();
					top.layer.msg(""+data[0].MSG+"");
				}else{
					layer.closeAll();
					top.layer.msg(""+data[0].MSG+"");
				}
			}
		});
	});
}
//定时刷新数据
function myrefresh(){
  inituserdata(pageRecords,pageNo);
}
setInterval(myrefresh, 5000);

function searchinfo(){
	pageNo=1;
	isjump=0;
	inituserdata(pageRecords,pageNo);
}
//选择使用类型
function select_vip_type(){
	var options=$("#user_type option:selected");
	//alert(options.val());
	is_use=options.val();
}
//选择冻结状态
function select_forcard_type(){
	var options=$("#forcard_type option:selected");
	//alert(options.val());
	is_export=options.val();
}




//删除数据
function deletecard(dc){
	var cardid=$(dc).attr("id");
	top.layer.confirm('确认删除？',{icon: 3,title:'提示信息'},function(index){
	var index = top.layer.msg('删除中，请稍候',{icon: 16,time:false,shade:0.8});
		$.ajax({
			url: "../../../../service",
			cache: false,
			async: false,
			data: {
				proc: "xmy_zyr.deletecard",
				para: "['" + loginname + "','" + loginkey + "','"+cardid+"']"
			},
			dataType: "json",
			success: function(data) {
				if(data[0].TAG=='1'){
					layer.closeAll();
					top.layer.msg(""+data[0].MSG+"");
					//alert(data[0].MSG);
					$(dc).parent().parent().remove();
				}
			}
		});
	});

}
function onclick_exeropt(){
		var is_use=$("#user_type option:selected").val();
		var is_export=$("#forcard_type option:selected").val();
		var card_cami=$("#card_camis").val();
		top.layer.confirm('确定导出体验卡？',{icon: 3,title:'提示信息'},function(index){
			var index = top.layer.msg('导出中，请稍候',{icon: 16,time:false,shade:0.8});
			setTimeout(function(){
				window.location.href="../../../../export/exp/experience_card.jsp?loginname="+loginname+"&loginkey="+loginkey+"&is_use="+is_use+"&is_export="+is_export+"&card_cami"+card_cami;
				top.layer.closeAll();
			},1000);
		})

 }
